﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

public partial class import : System.Web.UI.Page
{
    string t;
    protected void Page_Load(object sender, EventArgs e)
    {
        string error = "";
        string msg = "";
        HttpPostedFile fuFile = Request.Files[0];
        string fn = fuFile.FileName.Trim();
        Hashtable ht = new Hashtable();
        if (fn == "")
        {
            error = "请选定上传文件";
            Response.Write("{error:\"" + error + "\",msg:\"" + msg + "\"}");
            Response.End();
        }
        if (fuFile.ContentLength > Params.MaxFileSize)
        {
            error = "上传文件大小不能超过" + Params.MaxFileSize.ToString() + "字节";
            Response.Write("{error:\"" + error + "\",msg:\"" + msg + "\"}");
            Response.End();
        }
        string exten = System.IO.Path.GetExtension(fn).Trim().ToLower();
        if (exten != ".xls")
        {
            error = "上传的文件必须为Excel";
            Response.Write("{error:\"" + error + "\",msg:\"" + msg + "\"}");
            Response.End();
        }
        string fp = Params.FilesFolderPath + Fun.GetFileName() + exten;
        try
        {
            fuFile.SaveAs(fp);
        }
        catch (Exception ee)
        {
            error = ee.Message;
            Response.Write("{error:\"" + error + "\",msg:\"" + msg + "\"}");
            Response.End();
        }
        t = Server.UrlDecode(Request.Params["type"]).Trim().ToLower();
        DataTable dt = Fun.CreateDtFromExcel(fp);
        switch (t)
        {
            case LwUser.TeamType:
                this.ImportTeam(ref dt, ref error, ref msg);
                break;
            case LwUser.DoctorType:
                this.ImportDoctor(ref dt, ref error, ref msg);
                break;
            case LwUser.StudentType:
                this.ImportStudent(ref dt, ref error, ref msg);
                break;
            case Category.CategoryStr:
                this.ImportCategory(ref dt, ref error, ref msg);
                break;
            default:
                error = "发生错误";
                break;
        }
        Response.Write("{error:\"" + error + "\",msg:\"" + Server.HtmlEncode(msg) + "\"}");
        Response.End();
    }

    private void ImportTeam(ref DataTable dt,ref string error, ref string msg)
    {
        if (!Fun.CheckTable(ref dt, "id", "name", "email", "yjfx"))
        {
            error="Excel格式错误，表中必须有xm,je,sj三列";
            return;
        }
        string tip="";
        string fn= Fun.GetFileName() + ".txt";
        string fp = Params.FilesFolderPath +fn;
        FileStream fs = new FileStream(fp, FileMode.CreateNew);
        StreamWriter sw = new StreamWriter(fs);
        sw.WriteLine("ID\t说明");
        sw.WriteLine("----------------------------------------");
        int n = 0;
        foreach (DataRow dr in dt.Rows)
        {
            tip="";
            if (Team.Add(dr["id"].ToString().Trim(), dr["name"].ToString().Trim(), dr["email"].ToString().Trim(), dr["yjfx"].ToString().Trim(), ref tip) == 0)
            {
                sw.WriteLine(dr["id"].ToString().Trim() + "\t创建失败，原因：" + tip);
            }
            else
            {
                sw.WriteLine(dr["id"].ToString().Trim() + "\t创建成功");
                n++;
            }
        }
        sw.Close();
        fs.Close();
        msg = n.ToString()+","+Params.RootHttp+"download.aspx/"+fn+"?fp="+Server.UrlEncode(fp);
    }

    private void ImportDoctor(ref DataTable dt, ref string error, ref string msg)
    {
        if (!Fun.CheckTable(ref dt, "id", "name", "email","team","expert","yjfx"))
        {
            error="Excel格式错误，表中必须有id,name,email,team,expert,yjfx六列";
            return;
        }
        string tip = "";
        string fn = Fun.GetFileName() + ".txt";
        string fp = Params.FilesFolderPath + fn;
        FileStream fs = new FileStream(fp, FileMode.CreateNew);
        StreamWriter sw = new StreamWriter(fs);
        sw.WriteLine("ID\t说明");
        sw.WriteLine("----------------------------------------");
        int n = 0;
        foreach (DataRow dr in dt.Rows)
        {
            tip = "";
            if (Doctor.Add(dr["id"].ToString().Trim(), dr["name"].ToString().Trim(), dr["email"].ToString().Trim(),dr["team"].ToString().Trim(),dr["expert"].ToString().Trim(), ref tip) == 0)
            {
                sw.WriteLine(dr["id"].ToString().Trim() + "\t创建失败，原因：" + tip);
            }
            else
            {
                sw.WriteLine(dr["id"].ToString().Trim() + "\t创建成功");
                n++;
            }
        }
        sw.Close();
        fs.Close();
        msg = n.ToString() + "," + Params.RootHttp + "download.aspx/" + fn + "?fp=" + Server.UrlEncode(fp);
    }

    private void ImportStudent(ref DataTable dt, ref string error, ref string msg)
    {
        if (!Fun.CheckTable(ref dt, "id", "name", "email", "doctor"))
        {
            error = "Excel格式错误，表中必须有id,name,email,doctor四列";
            return;
        }
        string tip = "";
        string fn = Fun.GetFileName() + ".txt";
        string fp = Params.FilesFolderPath + fn;
        FileStream fs = new FileStream(fp, FileMode.CreateNew);
        StreamWriter sw = new StreamWriter(fs);
        sw.WriteLine("ID\t说明");
        sw.WriteLine("----------------------------------------");
        int n = 0;
        foreach (DataRow dr in dt.Rows)
        {
            tip = "";
            if (Student.Add(dr["id"].ToString().Trim(), dr["name"].ToString().Trim(), dr["email"].ToString().Trim(), dr["doctor"].ToString().Trim(), ref tip) == 0)
            {
                sw.WriteLine(dr["id"].ToString().Trim() + "\t创建失败，原因：" + tip);
            }
            else
            {
                sw.WriteLine(dr["id"].ToString().Trim() + "\t创建成功");
                n++;
            }
        }
        sw.Close();
        fs.Close();
        msg = n.ToString() + "," + Params.RootHttp + "download.aspx/" + fn + "?fp=" + Server.UrlEncode(fp);
    }

    private void ImportCategory(ref DataTable dt, ref string error, ref string msg)
    {
        if (!Fun.CheckTable(ref dt, "name", "intro"))
        {
            error = "Excel格式错误，表中必须有name,intro两列";
            return;
        }
        string tip = "";
        string fn = Fun.GetFileName() + ".txt";
        string fp = Params.FilesFolderPath + fn;
        FileStream fs = new FileStream(fp, FileMode.CreateNew);
        StreamWriter sw = new StreamWriter(fs);
        sw.WriteLine("Name\t说明");
        sw.WriteLine("----------------------------------------");
        int n = 0;
        foreach (DataRow dr in dt.Rows)
        {
            tip = "";
            if (Category.Add(dr["name"].ToString().Trim(), dr["intro"].ToString().Trim(), ref tip) == 0)
            {
                sw.WriteLine(dr["name"].ToString().Trim() + "\t创建失败，原因：" + tip);
            }
            else
            {
                sw.WriteLine(dr["name"].ToString().Trim() + "\t创建成功");
                n++;
            }
        }
        sw.Close();
        fs.Close();
        msg = n.ToString() + "," + Params.RootHttp + "download.aspx/" + fn + "?fp=" + Server.UrlEncode(fp);
    }
}
